"use strict";

import Vue from 'vue';
import axios from "axios";
import router from "@/router";
import {serverIp} from "../../public/config";
//axios.defaults.baseURL = "http://localhost:9090/";
// Full config:  全局配置
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
    baseURL: "http://" + serverIp + ":9090/",
    // baseURL: "http://localhost:9090/",
    timeout: 60 * 1000, // Timeout
    //withCredentials: true, // Check cross-site Access-Control
};

const request = axios.create(config);

// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token，对请求参数统一加密
request.interceptors.request.use(
    function (config) {
        // Do something before request is sent 发送请求前做点事
        config.headers['Content-Type'] = 'application/json;charset=utf-8';
        let user = sessionStorage.getItem("store") ? JSON.parse(sessionStorage.getItem("store")) : null
        if (user) {
            config.headers['token'] = user.admin.token;  // 设置请求头
            //config.headers['token'] = user.token;  // 设置请求头
        }
        return config;
    },
    function (error) {
        // Do something with request error
        //return Promise.reject(error);
    }
);

// Add a response interceptor
request.interceptors.response.use(
    function (response) {
        // Do something with response data

        return response;
    },
    function (error) {
        // Do something with response error
        console.log(error)
        return Promise.reject(error);
    }
);

Plugin.install = function (Vue, options) {
    Vue.axios = request;
    window.axios = request;
    Object.defineProperties(Vue.prototype, {
        axios: {
            get() {
                return request;
            }
        },
        $axios: {
            get() {
                return request;
            }
        },
    });
};

Vue.use(Plugin)

export default Plugin;
